<template>
  <el-container>
    <el-main v-loading="loading">
      <el-table :data="branchList">
        <el-table-column prop="id" label="分院ID" width='120'/>
        <el-table-column prop="name" label="分院名称"/>
        <el-table-column prop="area" label="地区" />
        <el-table-column prop="address" label="分院地址" />
        <el-table-column prop="workTime" label="营业时间" />
        <el-table-column prop="phone" label="联系电话" />
        <el-table-column label="操作" min-width="100">
          <template slot-scope="scope">
            <el-button @click="handleClick(scope.row)" type="text" size="small">编辑</el-button>
            <el-button @click="stopClick(scope.row)" type="text" size="small">{{scope.row.deleteFlag ? '下线': '上线'}}</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-dialog
        title="提示"
        :visible.sync="dialogVisible"
        width="30%">
        <span>{{dialogContent}}</span>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="sure">确 定</el-button>
        </span>
      </el-dialog>
    </el-main>
  </el-container>
</template>
<script>
const tips = ['确认上线该分院信息?','确认下线该分院信息?','确认删除该分院信息?'];
let delUrls = ['/physical','/chinaMed','/dentistry'];
let stopUrls = ['/physicalChild','/chinaMedChild','/dentistryChild'];
export default {
  name: "branchTable",
  props:['data', 'loading'],
  data() {
    return {
      branchList: [],
      dialogContent: '',
      dialogVisible: false,
      id:'',
      code:'',
      codeId:'',
    };
  },
  mounted(){
    if( this.$route.query ){
      this.code = this.$route.query.code;
      this.codeId = this.$route.query.id;
    } 
  },
  methods: {
    handleClick(params) {
      this.id = params.id;
      this.$router.push(`branchAdd?type=edit&code=${this.code}&codeId=${this.codeId}&id=${this.id}`);
    },  
    stopClick(params) {
      this.id = params.id;
      this.dialogVisible = true;
      if(params.deleteFlag) return this.dialogContent = tips[1];
      this.dialogContent = tips[0];
    },
    sure(){
      let url = stopUrls[this.code];
      let user = this.lodash.filter(this.branchList, item =>{
        return item.id === this.id;
      })
      let { name, contactPerson, address, workTime, phone, deleteFlag, id, province, city, country } = user[0];
      let codeId = this.codeId;
      let code = this.code;
      deleteFlag = !deleteFlag;
      let param = {deleteFlag, id, name, contactPerson, address, workTime, phone, province, city, country};
      if( code === "0" ) param.physicalId = codeId;
      if( code === "1" ) param.chinaMedId = codeId;
      if( code === "2" ) param.dentistryId = codeId;
      this.api.put(`${url}`, param, 'form').then(()=>{
        this.$emit("search");
      }).catch((err) => {
        console.log(err)
        this.$message(err.data.message)
      })
      this.dialogVisible = false;
    }
  },
  watch:{
    data(newValue){
      this.branchList = newValue;
    }
  }
};
</script>
<style lang="less" scoped>
</style>